<template>
  <div class="auto-reply">
    <!-- 添加关键词回复 -->
    <el-form
      size="mini"
      label-width="100px"
      ref="addReplyRef"
      :rules="rules"
      :model="form"
      class="el-form-my"
    >
      <el-form-item class="el-form-item-my" label="关键词：" prop="keyWord">
        <el-input
          v-model="form.keyWord"
          maxlength="20"
          show-word-limit
          clearable
          placeholder="输入关键词"
        ></el-input>
      </el-form-item>
      <el-form-item class="el-form-item-my" label="回复内容：" prop="replyMsg">
        <el-input
          type="textarea"
          v-model="form.replyMsg"
          maxlength="500"
          :autosize="{ minRows: 4, maxRows: 4}"
          resize="none"
          show-word-limit
          clearable
          placeholder="回复内容不能为空"
        ></el-input>
      </el-form-item>

      <el-form-item size="medium" class="add-btn">
        <el-button type="primary" @click="onSubmit()">立即添加</el-button>
        <!-- <el-button @click="resetForm('addFriendFormRef')">重置</el-button> -->
      </el-form-item>
    </el-form>

    <!-- 管理 -->
    <!-- {{autoReply}} -->
    <el-table :data="autoReply" stripe border height="60" class="table-data" style="width: 100%">
      <el-table-column label="关键词" prop="keyWord"></el-table-column>
      <el-table-column label="回复内容" prop="replyMsg"></el-table-column>
      <el-table-column label="消息类型" prop="msgType" width="120px"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="updateReply(scope)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteReply(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'ToolsAutoReply',
  data() {
    return {
      // 要提交的表单
      form: {
        keyWord: '',
        replyMsg: '',
        msgType: 'Text',
      },
      // 表单验证
      rules: {
        keyWord: [
          { required: true, message: '请输入关键词', trigger: 'blur' },
          //   { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        replyMsg: [
          { required: true, message: '请输入回复的话术', trigger: 'blur' },
        ]
      },
    }
  },
  computed: {
    ...mapGetters({
      userId: 'userId',
      autoReply: 'nedb/autoReply',
    })
  },
  methods: {
    // 提交表单
    onSubmit() {
      // 默认对所有的微信生效
      // 如果通过验证 提交表单
      this.$refs['addReplyRef'].validate((valid) => {
        if (valid) {
          this.$store.dispatch('nedb/UpdateOrAddAutoReply', this.form)
          // 重置表单
          //   this.resetForm()
        } else {
          this.$message({
            message: '请检查表单！',
            type: 'error'
          })
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName = 'addReplyRef') {
      this.$refs[formName].resetFields();
    },
    // 删除自动回复
    deleteReply(scope) {
      this.$confirm('确定删除 ?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认退出后，向服务端通知
        this.$store.dispatch('nedb/DeleteAutoReply', scope.row)
      }).catch(() => {
        return false
      });
    },
    // 编辑回复内容
    updateReply(scope) {
      for (const key in this.form) {
        this.$set(this.form, key, scope.row[key])
      }
    },
  },
  created() {
    // 读取配置
    // this.$store.dispatch('nedb/SetAutoReply')
  }
}
</script>

<style lang="scss" scoped>
.auto-reply {
  height: 100%;
  display: flex;
  flex-direction: column;

  .el-form-my {
    position: relative;
    border-bottom: solid black 5px;

    .el-form-item-my {
      width: 80%;
      margin-bottom: 0;
      margin: 20px 0;
    }

    .add-btn {
      position: absolute;
      right: 5%;
      top: 50%;
      transform: translate(-5%, -50%);
    }
  }
}
</style>
